package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.OnlineUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/system/user/status")
public class UserStatusController {
    @Autowired
    private OnlineUserService onlineUserService;


    // 获取在线用户列表
    @GetMapping("/onlineUsers")
    public AjaxResult getOnlineUsers() {
        return AjaxResult.success(onlineUserService.getOnlineUserIds());
    }

    //记录人员记录
    @GetMapping("/addOnlineUsers")
    public AjaxResult addOnlineUsers(Integer userId) {
        return AjaxResult.success(onlineUserService.userLogin(userId));
    }
    //删除人员记录

    @GetMapping("/delOnlineUsers")
    public AjaxResult delOnlineUsers(Integer userId) {
        return AjaxResult.success(onlineUserService.userLogout(userId));
    }


    // 检查指定用户是否在线
    @GetMapping("/isOnline")
    public AjaxResult isUserOnline(Integer userId) {
        return AjaxResult.success(onlineUserService.isUserOnline(userId));
    }

    // 更新当前用户活动时间
    @PostMapping("/updateActivity")
    public AjaxResult updateActivity() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser != null) {
            onlineUserService.updateUserActivity(Math.toIntExact(loginUser.getUserId()));
        }
        return AjaxResult.success();
    }

}
